816B - Karen and Coffee - CodeForces Solution


binary search data structures implementation *1400

Please click on ads to support us..

Python Code:

import collections
import heapq
import sys
import math
import itertools
import bisect
from io import BytesIO, IOBase
import os


def value(): return tuple(map(int, input().split()))
def values(): return tuple(map(int, sys.stdin.readline().split()))
def inlst(): return [int(i) for i in input().split()]
def inlsts(): return [int(i) for i in sys.stdin.readline().split()]
def inp(): return int(input())
def inps(): return int(sys.stdin.readline())
def instr(): return input()
def stlst(): return [i for i in input().split()]



def solve():
    n, k, q = values()
    l = [0]*(200003)
    mn, mx = 200003, 0
    for i in range(n):
        a, b = values()
        mn = min(mn, a)
        mx = max(mx, b)
        l[a] += 1
        l[b+1] -= 1
    arr = [0]*(200003)
    for i in range(200001):
        l[i+1] += l[i]
        if l[i] >= k:
            arr[i] = 1

    for i in range(200001):
        arr[i+1] += arr[i]
    for _ in range(q):
        a, b = values()
        tot = arr[b]-arr[a-1]
                        sys.stdout.write(str(tot)+"\n")


solve()

C++ Code:

#include <iostream>
#include <algorithm>
#define For(i,n,uu) for(int i=uu;i<n;++i)
using namespace std;
typedef long long ll;
typedef long  double ld;
const int Max_N=200001;
int D[Max_N],sum[Max_N];
void reset(int s)
{
    For(i,Max_N,0)
    D[i]=s;
}
main ()
{
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int n,k,q; cin>>n>>k>>q;//tinh tu 1 -> 200000
    reset(0);
    For(i,n,0)
    {
        int a,b; cin>>a>>b;
        D[a-1]++;
        if (b<200000) D[b]--;
    }
    sum[0]=0;
    int a0=0;
    For(i,Max_N,1)
    {
    sum[i]=sum[i-1]+( a0 + D[i-1] >= k);
    a0+=D[i-1];
    }
    //solve
     For(i,q,0)
     {
         int a,b;
         cin>>a>>b;
         cout<<sum[b]-sum[a-1]<<endl;
     }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing